package com.gdhz.xiaohengqin.device.model.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * @author Swallows
 * @date 2025-06-04 09:48
 */
@NoArgsConstructor
@AllArgsConstructor
@Table("access_gate")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AccessGate implements Serializable {

    private final static ObjectMapper objectMapper = new ObjectMapper();

    @Serial
    private static final long serialVersionUID = 1L;

    @Id
    @Getter
    @Setter
    private Integer sn;

    @Getter
    @Setter
    private String name;

    @Getter
    @Setter
    private Integer maxDoor;

    /**
     * 0 在线 1 离线
     */
    @Getter
    @Setter
    private Integer status = 1;

    @Getter
    @Setter
    private LocalDateTime refreshTime;

    private String doorInfo;

    public List<DoorInfo> getDoorInfo() throws JsonProcessingException {
        return doorInfo == null ?
                Collections.emptyList()  :
                objectMapper.readValue(doorInfo, new TypeReference<>() {
                });
    }

    public void setDoorInfo(List<DoorInfo> doorInfo) throws JsonProcessingException {
        this.doorInfo  = objectMapper.writeValueAsString(doorInfo);
    }
}
